#region License

/*
  GShield - Game Shield
  Copyright (C) 2006-2007 WaveTech Digital Technologies, Inc.
                                http://www.wtdt.com

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#endregion License

#region Includes

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Windows.Forms;
using CristiPotlog.Controls;
using WaveTech.GShield.Common;
using WaveTech.GShield.Data;
using WaveTech.GShield.Engine.Managers;

#endregion

namespace WaveTech.GShield.Manager
{
	/// <summary>
	/// Initial Setup Wizard
	/// </summary>
	public partial class Setup : Form
	{
		#region Constructor

		public Setup()
		{
			InitializeComponent();
		}

		#endregion Construcotr

		#region wizard1_Cancel

		private void wizard1_Cancel(object sender, CancelEventArgs e)
		{
			Environment.Exit(0);
		}

		#endregion wizard1_Cancel

		#region wizard1_BeforeSwitchPages

		private void wizard1_BeforeSwitchPages(object sender, Wizard.BeforeSwitchPagesEventArgs e)
		{
			if (e.OldIndex == 1)
			{
				if (txtPassword1.Text.Length <= 0 || txtPassword2.Text.Length <= 0)
				{
					MessageBox.Show("You must supply a password.", "GShield Setup", MessageBoxButtons.OK, MessageBoxIcon.Error);
					e.Cancel = true;
				}
				else if (txtPassword1.Text != txtPassword2.Text)
				{
					MessageBox.Show("Your passwords do not match.", "GShield Setup", MessageBoxButtons.OK, MessageBoxIcon.Error);
					e.Cancel = true;
				}
				else if (txtPassword1.Text.Length < 4)
				{
					MessageBox.Show("Your passwords should be at least 4 characters.", "GShield Setup", MessageBoxButtons.OK,
					                MessageBoxIcon.Error);
					e.Cancel = true;
				}
			}
			else if (e.OldIndex == 2)
			{
				if (cboQuestion1.SelectedItem == null || cboQuestion2.SelectedItem == null)
				{
					MessageBox.Show("You must select some forgetten password questions.", "GShield Setup", MessageBoxButtons.OK,
					                MessageBoxIcon.Error);
					e.Cancel = true;
				}
				else if (txtAnswer1.Text.Length <= 0 || txtAnswer2.Text.Length <= 0)
				{
					MessageBox.Show("You must supply forgetten password questions answers.", "GShield Setup", MessageBoxButtons.OK,
					                MessageBoxIcon.Error);
					e.Cancel = true;
				}
				else if (cboQuestion1.SelectedItem == cboQuestion2.SelectedItem)
				{
					MessageBox.Show("You must select different forgetten password questions.", "GShield Setup", MessageBoxButtons.OK,
					                MessageBoxIcon.Error);
					e.Cancel = true;
				}
			}
			else if (e.OldIndex == 3)
			{
			}
		}

		#endregion wizard1_BeforeSwitchPages

		#region wizard1_Finish

		private void wizard1_Finish(object sender, EventArgs e)
		{
			Cursor.Current = Cursors.WaitCursor;

			Setting s = SettingsDAO.Get();
			s.AdminEmail = txtEmailAddress.Text;
			s.AdminPassword = Hash.ComputeHash(txtPassword2.Text.Trim(), "SHA256", null);
			s.LPQ1Id = LpqDAO.GetIdByQuestion(cboQuestion1.SelectedItem.ToString());
			s.LPQ1Ans = txtAnswer1.Text;
			s.LPQ2Id = LpqDAO.GetIdByQuestion(cboQuestion2.SelectedItem.ToString());
			s.LPQ2Ans = txtAnswer2.Text;
			s.SmtpServer = txtSmtpServer.Text;
			s.SmtpRequireLogin = chkRequiredLogin.Checked;
			s.SmtpLogin = txtSmtpLogin.Text;
			s.SmtpPassword = txtSmtpPassword.Text;

			SettingsDAO.Update(s);

			Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

			config.AppSettings.Settings.Remove("UnhandledExceptionManager/EmailTo");
			config.AppSettings.Settings.Add("UnhandledExceptionManager/EmailTo", "gshielderrors@wtdt.com");

			config.AppSettings.Settings.Remove("UnhandledExceptionManager/DefaultDomain");
			config.AppSettings.Settings.Add("UnhandledExceptionManager/DefaultDomain", "wtdt.com");

			config.AppSettings.Settings.Remove("UnhandledExceptionManager/MailServer");
			config.AppSettings.Settings.Add("UnhandledExceptionManager/MailServer", s.SmtpServer);

			config.AppSettings.Settings.Remove("UnhandledExceptionManager/MailServerPort");
			config.AppSettings.Settings.Add("UnhandledExceptionManager/MailServerPort", "25");

			config.AppSettings.Settings.Remove("UnhandledExceptionManager/MailServerUsername");
			config.AppSettings.Settings.Add("UnhandledExceptionManager/MailServerUsername", s.SmtpLogin);

			config.AppSettings.Settings.Remove("UnhandledExceptionManager/MailServerPassword");
			config.AppSettings.Settings.Add("UnhandledExceptionManager/MailServerPassword", s.SmtpPassword);

			config.Save(ConfigurationSaveMode.Modified);
			ConfigurationManager.RefreshSection("appSettings");

			MDIMain main = new MDIMain();
			Cursor.Current = Cursors.Default;

			ServiceManager.StartSentinelService();

			main.Show();
			Hide();
		}

		#endregion wizard1_Finish

		#region Setup_Load

		private void Setup_Load(object sender, EventArgs e)
		{
			List<string> lst = LpqDAO.GetAllQuestions();
			cboQuestion1.Items.AddRange(lst.ToArray());
			cboQuestion2.Items.AddRange(lst.ToArray());
		}

		#endregion Setup_Load

		#region chkRequiredLogin_CheckedChanged

		private void chkRequiredLogin_CheckedChanged(object sender, EventArgs e)
		{
			if (chkRequiredLogin.Checked)
			{
				txtSmtpLogin.Enabled = true;
				txtSmtpPassword.Enabled = true;
			}
			else
			{
				txtSmtpLogin.Enabled = false;
				txtSmtpPassword.Enabled = false;
			}
		}

		#endregion chkRequiredLogin_CheckedChanged
	}
}